package ml.coursedesign.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import ml.coursedesign.common.lang.Result;
import ml.coursedesign.common.util.JWTUtils;
import ml.coursedesign.entity.*;
import ml.coursedesign.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    SelectSubjectService selectSubjectService;
    @Autowired
    StudentCourseService studentCourseService;
    @Autowired
    SubjectService subjectService;
    @Autowired
    SubjectGroupService subjectGroupService;

    /**
     * 学生登录
     * @param student
     * @return
     */
    @PostMapping(value = "/login",produces = "application/json")
    public Result stuLogin(@RequestBody Student student){
        String token = null;

        String stuId = student.getStuName();
        Student newStudent = studentService.getById(stuId);
        if(student.getPassword().endsWith(newStudent.getPassword())){
              token = new JWTUtils().createToken(stuId,"教师");
        }
        System.out.println(token);
        System.out.println(new JWTUtils().getUsernameFromToken(token));
         return Result.succ(token);
    }

    @GetMapping(value = "get/group/info/by/{stuId}")
    public Result getGroupInfoByStudent(@PathVariable String stuId){
        QueryWrapper<SelectSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",stuId);
        SelectSubject selectSubject = selectSubjectService.getOne(queryWrapper);
        String subjectId = selectSubject.getSubjectId();
        String classId = selectSubject.getCourseClassId();
        System.err.println(subjectId+"++"+classId);
        GroupVo groupVo = subjectGroupService.getSubjectGroupnfo(subjectId, classId);


        return Result.succ(groupVo);

    }
    /**
     * 更具学号获取
     * @param stuId
     * @return
     */
    @GetMapping(value = "get/student/info/{stuId}")
    public Result getStudentInfo(@PathVariable String stuId ){
        System.err.println(stuId+"");
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",stuId);
        Student student = studentService.getOne(queryWrapper);
        System.err.println(student);
        return Result.succ(student);

    }




    /**
     * 学生选着课题信息
     * @param subjectId
     * @param stuId
     * @return
     */
    @GetMapping(value = "/select/subject/{subjectId}/{stuId}",produces = "application/json")
    public Result stuSelectSubject(@PathVariable("subjectId") String subjectId,
                                   @PathVariable("stuId") String stuId){

        String classId = getClassId(stuId);
        SelectSubject data = new SelectSubject();
        data.setSubjectId(subjectId);
        data.setStuId(stuId);
        data.setCourseClassId(classId);
        boolean b = selectSubjectService.save(data);
        if (b ){
            return Result.succ();
        }else{
            return Result.fail();
        }
    }

    /**
     * 获得学生选择的课题
     * @param stuId
     * @return
     */

    @GetMapping(value = "/get/select/subject/{stuId}",produces = "application/json")
    public Result getSubject(@PathVariable("stuId") String stuId){

        QueryWrapper<SelectSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",stuId);
        SelectSubject selectSubject = selectSubjectService.getOne(queryWrapper);
        return Result.succ(selectSubject);

    }
    @GetMapping("/get/stuName/{username}")
    public Result getStudnetName(@PathVariable String username){
        String name="";
        QueryWrapper<Student > queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Student student = studentService.getOne(queryWrapper);
        if(student!=null){
           name=student.getStuName() ;
        }
       return Result.succ(name);
    }

    /**
     * 学生取消选着的课题
     * @param stuId
     * @return
     */

    @GetMapping(value = "/cancel/select/subject/{stuId}",produces = "application/json")
    public Result cancelSelectSubject(@PathVariable("stuId") String stuId){



        QueryWrapper<SelectSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",stuId);
        String subjectId = getSubjectId(stuId);
        boolean isRemove = selectSubjectService.remove(queryWrapper);

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("username",stuId);
        Student one = studentService.getOne(studentQueryWrapper);
        one.setIdentity(0);
        boolean update = studentService.updateById(one);


        if(isRemove && update) {
            return Result.succ();
        }else{
            return Result.fail();
        }



    }

    /**
     * 更具学号获得课程号
     * @param subId
     * @return
     */
    private String getSubjectId(String subId){
        QueryWrapper<SelectSubject> querWrapper = new QueryWrapper<>();
        querWrapper.eq("stu_id",subId);
        SelectSubject selectSubject= selectSubjectService.getOne(querWrapper);
        String subjectId1 = selectSubject.getSubjectId();
        return subjectId1;
    }


    /**
     * 根据学号获得班课号
     * @param subId
     * @return
     */
    private String getCourseClassId(String subId){
        QueryWrapper<StudentCourse> querWrapper = new QueryWrapper<>();
        querWrapper.eq("student_id",subId);
        StudentCourse studentCourse= studentCourseService.getOne(querWrapper);
        String studentCourseId = studentCourse.getCourseClassId();
        return studentCourseId;
    }

    private String getClassId(String subId){
        QueryWrapper<Student> querWrapper = new QueryWrapper<>();
        querWrapper.eq("username",subId);
        Student student= studentService.getOne(querWrapper);
        String classid = student.getClassName();
        return String.valueOf(classid.charAt(classid.length()-2));
    }

    /**
     * 添加已选人数
     * @param subjectId
     * @return
     */






    /*private boolean subjectAdd(String subjectId){
        Subject subject = subjectService.getById(subjectId);
        int selectedNum = subject.getSelectedNum()+1;
        subject.setSelectedNum(selectedNum);
        boolean save = subjectService.saveOrUpdate(subject);
        return save;

    }
*/
    /**
     * 删除已选人数
     * @param subjectId
     * @return
     */
   /* private boolean subjectRemove(String subjectId){
        Subject subject = subjectService.getById(subjectId);
        int selectedNum = subject.getSelectedNum()-1;
        subject.setSelectedNum(selectedNum);
        boolean remove = subjectService.saveOrUpdate(subject);
        return remove;

    }*/


}

